﻿#pragma once

#include "../../MirControls/MirImageControl.h"
#include "../../MirControls/MirButton.h"
#include "../../MirControls/MirLabel.h"
#include "../../../Shared/Data/ClientData.h"
#include "../../MirControls/MirControl.h"
#include "../../MirControls/MirTextBox.h"
#include "../../MirControls/MirItemCell.h"
#include "../../../Shared/Data/ItemData.h"
#include <string>
#include <vector>
#include <cmath>
#include <any>
#include "../../../stringhelper.h"

//C# TO C++ CONVERTER NOTE: Forward class declarations:
namespace Client::MirScenes::Dialogs { class MailItemRow; }

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirSounds;
using namespace Client::MirNetwork;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{
    class MailListDialog : public MirImageControl
    {
    private:
        MirImageControl *TitleLabel;
        MirButton *HelpButton, *CloseButton;
        MirLabel *TitleTypeLabel, *TitleSenderLabel, *TitleMessageLabel;
        //Send / Reply (Can only reply if index exists, button will disapear if not) / Read / Delete / Block List / Bug Report (new system??)

        MirLabel *PageLabel;
        MirButton *PreviousButton, *NextButton;
        MirButton *SendButton, *ReplyButton, *ReadButton, *DeleteButton, *BlockListButton, *BugReportButton;

    public:
        std::vector<MailItemRow*> Rows = std::vector<MailItemRow*>(10);

        ClientMail *SelectedMail;
        int SelectedIndex = 0;

    private:
        int StartIndex = 0;
        int CurrentPage = 1, PageCount = 1;

    public:
        virtual ~MailListDialog()
        {
            delete TitleLabel;
            delete HelpButton;
            delete CloseButton;
            delete TitleTypeLabel;
            delete TitleSenderLabel;
            delete TitleMessageLabel;
            delete PageLabel;
            delete PreviousButton;
            delete NextButton;
            delete SendButton;
            delete ReplyButton;
            delete ReadButton;
            delete DeleteButton;
            delete BlockListButton;
            delete BugReportButton;
            delete SelectedMail;
        }

        MailListDialog();

        void Reset();

        void UpdateInterface();

        int FindSelectedIndex();

        void UpdateRows();

        void Show() override;

        void Hide() override;

        void Toggle();
    };

    class MailItemRow : public MirControl
    {
    public:
        ClientMail *Mail = nullptr;

        MirLabel *SenderLabel, *MessageLabel;
        MirImageControl *IconImage, *UnreadImage, *ParcelImage, *LockedImage, *SelectedImage;

        bool Selected = false;

    private:
        Size *IconArea = new Size(34, 32);

    public:
        virtual ~MailItemRow()
        {
            delete Mail;
            delete SenderLabel;
            delete MessageLabel;
            delete IconImage;
            delete UnreadImage;
            delete ParcelImage;
            delete LockedImage;
            delete SelectedImage;
            delete IconArea;
        }

        MailItemRow();

    private:
        void QuestRow_BeforeDraw(std::any sender, EventArgs *e);

    public:
        void UpdateInterface();

    protected:
        void Dispose(bool disposing) override;

        void OnMouseEnter() override;
        void OnMouseLeave() override;
    };

    class MailComposeLetterDialog : public MirImageControl
    {
    private:
        MirLabel *RecipientNameLabel;
        MirTextBox *MessageTextBox;
        MirButton *SendButton, *CancelButton, *CloseButton;

    public:
        virtual ~MailComposeLetterDialog()
        {
            delete RecipientNameLabel;
            delete MessageTextBox;
            delete SendButton;
            delete CancelButton;
            delete CloseButton;
        }

        MailComposeLetterDialog();

        void ComposeMail(const std::string &recipientName, const std::string &message = "");
    };
    class MailComposeParcelDialog : public MirImageControl
    {
    public:
        MirLabel *RecipientNameLabel, *ParcelCostLabel, *GoldSendLabel;
    private:
        MirTextBox *MessageTextBox;
        MirButton *StampButton, *SendButton, *CancelButton, *CloseButton;
        MirImageControl *ItemCover;

        static constexpr unsigned int _cellCount = 5;

    public:
        std::vector<MirItemCell*> Cells = std::vector<MirItemCell*>(_cellCount);

        static std::vector<UserItem*> Items;
        static std::vector<unsigned long long> ItemsIdx;

        unsigned int GiftGoldAmount = 0;
        bool Stamped = false;

        virtual ~MailComposeParcelDialog()
        {
            delete RecipientNameLabel;
            delete ParcelCostLabel;
            delete GoldSendLabel;
            delete MessageTextBox;
            delete StampButton;
            delete SendButton;
            delete CancelButton;
            delete CloseButton;
            delete ItemCover;
        }

        MailComposeParcelDialog();

        void Hide() override;

        void Reset();

        void ResetLockedCells();

    private:
        void StampParcel();

        void UpdateParcel();


    public:
        void CalculatePostage();

        void ComposeMail(const std::string &recipientName);
    };

    class MailReadLetterDialog : public MirImageControl
    {
    private:
        MirLabel *SenderNameLabel, *DateSentLabel, *MessageLabel;

        MirButton *DeleteButton, *LockButton, *CancelButton, *CloseButton;

    public:
        ClientMail *Mail;

        virtual ~MailReadLetterDialog()
        {
            delete SenderNameLabel;
            delete DateSentLabel;
            delete MessageLabel;
            delete DeleteButton;
            delete LockButton;
            delete CancelButton;
            delete CloseButton;
            delete Mail;
        }

        MailReadLetterDialog();

        void ReadMail(ClientMail *mail);
    };
    class MailReadParcelDialog : public MirImageControl
    {
    private:
        MirLabel *SenderNameLabel, *DateSentLabel, *MessageLabel, *GoldSendLabel;

        MirButton *CollectButton, *CancelButton, *CloseButton;

    public:
        std::vector<MirItemCell*> Cells = std::vector<MirItemCell*>(5);

        ClientMail *Mail;

        virtual ~MailReadParcelDialog()
        {
            delete SenderNameLabel;
            delete DateSentLabel;
            delete MessageLabel;
            delete GoldSendLabel;
            delete CollectButton;
            delete CancelButton;
            delete CloseButton;
            delete Mail;
        }

        MailReadParcelDialog();

        void ReadMail(ClientMail *mail);

        void ResetCells();
    };
}
